#论坛系统首页/主页
from common.com import ForumDriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
from tests import ForumLogin


class ForumList:
    driver = ""
    url = ""
    def __init__(self):
        self.url="http://127.0.0.1:58080/index.html"
        self.driver = ForumDriver.driver
        self.driver.get(self.url)


    def HomeTest(self):
        # 检查首页元素
        self.driver.find_element( By.CSS_SELECTOR, "#article_list_board_title" )
        self.driver.find_element( By.CSS_SELECTOR, "body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > h1 > a > img" )
        self.driver.find_element( By.CSS_SELECTOR, "#index_nav_avatar" )
        self.driver.find_element( By.CSS_SELECTOR, "#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post > svg" )
        self.driver.find_element( By.CSS_SELECTOR, "#artical-items-body > div:nth-child(2) > div > div.col" )
        ForumDriver.getScreenShot()  # 截图
        # 检查第一个帖子是否存在，帖子动态加载，所以显示等待
        WebDriverWait( self.driver, 5 ).until(EC.visibility_of_all_elements_located( (By.CSS_SELECTOR, "#artical-items-body > div:nth-child(1) > div > div.col") ))
        self.driver.find_element( By.CSS_SELECTOR, "#artical-items-body > div:nth-child(1) > div > div.col" )
        # 测试板块栏跳转，如跳到C++
        # 记录首页模块
        home_index = self.driver.find_element( By.CSS_SELECTOR, "#nav_board_index > a" )
        # 点击C++模块
        self.driver.find_element( By.CSS_SELECTOR, "#topBoardList > li.nav-item.active > a > span.nav-link-title" ).click()
        # 检查page_title是否为C++
        self.driver.find_element( By.CSS_SELECTOR, "#article_list_board_title" )

        # 返回首页
        home_index.click()
        # 测试发布帖子按钮
        self.driver.find_element( By.CSS_SELECTOR,"#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post" ).click()
        # 检查是否跳转到发新帖页面
        WebDriverWait( self.driver, 5 ).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "#bit-forum-content > div.page-header.d-print-none > div > div > div > h2")))
        text = self.driver.find_element( By.CSS_SELECTOR,"#bit-forum-content > div.page-header.d-print-none > div > div > div > h2" ).text
        assert text == "发新贴"
        #返回论坛首页
        home_index.click()
        # 点击第一个帖子 - 显示等待
        WebDriverWait( self.driver, 5 ).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "#artical-items-body > div:nth-child(1)") ))
        self.driver.find_element( By.CSS_SELECTOR,"#artical-items-body > div:nth-child(1)" ).click()
        time.sleep( 5 )
        # 检查是否跳转到帖子详情页面#details_article_title
        self.driver.find_element( By.CSS_SELECTOR, "#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong" )
        time.sleep( 3 )
        # 返回首页
        home_index.click()

